package com.jason.cloud.engine.share.driver.role.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jason.cloud.engine.share.driver.enterprise.bean.EnterpriseBean;
import com.jason.cloud.engine.share.driver.pack.service.PackServiceImpl;
import com.jason.cloud.engine.share.driver.role.bean.RoleBean;
import com.jason.cloud.engine.share.driver.role.dao.RoleDao;
import com.whalin.MemCached.MemCachedClient;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Resource
    private RoleDao roleDao;

    @Resource
    private MemCachedClient memCachedClient;

    /**
     * 根据ID查询对象
     * @param id
     * @return
     */
    public RoleBean queryRoleById(String id) {
        RoleBean roleBean = null;
        try {
            roleBean = this.roleDao.queryRoleById(id);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return roleBean;
    }

    /**
     * 拷贝默认角色
     * @param enterpriseId
     * @return
     */
    public void copyEnterpriseRoleByDefault(String enterpriseId) {
        try {
            this.roleDao.copyEnterpriseRoleByDefault(enterpriseId);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }

    /**
     * 根据企业ID查询默认的角色
     * @param enterpriseId
     * @return
     */
    public RoleBean queryDefaultRoleByEnterpriseId(String enterpriseId) {
        RoleBean roleBean = null;
        try {
            roleBean = this.roleDao.queryDefaultRoleByEnterpriseId(enterpriseId);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return roleBean;
    }

    /**
     * 列举所有角色
     * @return
     */
    public List<RoleBean> getRoleLimitByEnterpriseId(int page, int size, String enterpriseId) {
        List<RoleBean> roleBeanList = new ArrayList<RoleBean>();
        try {
            roleBeanList = this.roleDao.getRoleLimitByEnterpriseId(page, size, enterpriseId);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return roleBeanList;
    }

    /**
     * 计数角色
     * @return
     */
    public int getRoleCountByEnterpriseId(String enterpriseId) {
        int count = 0;
        try {
            count = this.roleDao.getRoleCountByEnterpriseId(enterpriseId);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return count;
    }

    /**
     * 增加角色
     * @param role
     */
     public void insertRole(RoleBean role) {
         try {
             this.roleDao.insertRole(role);
         } catch (Exception e) {
             logger.debug(e.getMessage());
         }
     }

    /**
     * 加载角色
     * @param enterpriseId
     * @return
     */
    public List<RoleBean> getRoleByEnterpriseId(@Param("enterpriseId") String enterpriseId) {
        List<RoleBean> roleBeanList = new ArrayList<RoleBean>();
        try {
            roleBeanList = this.roleDao.getRoleByEnterpriseId(enterpriseId);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return roleBeanList;
    }
}
